<?php
/**
 * Created by PhpStorm.
 * User: aDaddy
 * Date: 2016/7/25
 * Time: 9:55
 */

namespace App\Model;

use App\Base\BaseModel;
use App\Base\IRI;
use App\Entity\ComCategory;
use App\Setting\ComCategorySetting;
use Symfony\Component\HttpFoundation\Request;

class ComCategoryModel extends BaseModel{

    /*
     * 列表基本设置【通用模块必须】
     */
    public function listSettingDefault()
    {
        $settings = new ComCategorySetting($this->getDoctrine());
        return $settings->listConfig();
    }

    //获取所有分类
    public function getAllComCategory(){
        return $this->getRep()->findBy(array("parent"=>null),array("sort"=>"ASC"));
    }

    //获取所有分类并构建分类树
    public function getAllComCategoryTree()
    {
        $root = $this->getAllComCategory();
        $this->createTree($root,$list);
        $data = array();
        $levels = [];
        /**@var $en ComCategory */
        foreach ($list as $en) {

            if (!$en->getParent()){
                $level = 0;
            }else{
                $level = $levels[$en->getParent()->getId()] + 1;
            }
            $levels[$en->getId()] = $level;
            $prefix = "";
            if ($level > 0) {
                $prefix .="├";
                for ($i=0;$i<$level;$i++ ) {
                    $prefix .="────";
                }
            }
            $data[] = [
                "level" => $level,
                "data" => $en,
                "pre" => $prefix,
            ];
        }
        return $data;
    }

    function createTree($objectList , &$rst = [] ,$level = 0)
    {
        foreach ($objectList as $parent) {
            $rst[] = $parent;
            if ($sons= $parent->getSons()) {
                $this->createTree($sons,$rst,$level+1);
            }
        }
    }

    //获取第一层分类
    public function getRootComCategory()
    {
        $qb = $this->getQueryBuilder();
        $qb->select("ComCategory")
            ->where($qb->expr()->isNull("ComCategory.parent"))
            ->from("App:ComCategory","ComCategory");
        return $qb->getQuery()->execute();
    }


    public function getSons4Setting($parent=null)
    {

        $data = $this->getAllComCategoryTree();
        foreach ($data as $row) {
            if (!$parent) {
                $return[ IRI::createIRIStringFromEntity(ComCategory::class,$row["data"]->getId())] = $row["pre"] . $row["data"]->getName();
            }else{
                if ($row["data"]->getParent() && $row["data"]->getParent()->getId() == $parent) {
                    $return[ IRI::createIRIStringFromEntity(ComCategory::class,$row["data"]->getId())] = $row["pre"] . $row["data"]->getName();
                }
            }
        }
        return $return ;
    }

    /*
     * 通用Rep
     */
    public function getRep()
    {
        $settings = new ComCategorySetting($this->getDoctrine());
        return $this->em->getRepository($settings->bundleName.":".$settings->entityName);
    }


//    public function getSons($parent)
//    {
//        $qb = $this->getQueryBuilder();
//        $qb->select("ComCategory")
//            ->where("ComCategory.parent = :parent")
//            ->setParameter("parent",$parent)
//            ->from("App:ComCategory","ComCategory");
//
//        dd($qb->getQuery()->getSQL());
//        return $qb->getQuery()->execute();
//    }

}